P8105 HW4 Dashboard

library(tidyverse)
library(plotly)
library(flexdashboard)
library(p8105.datasets)

# Load dataset (choose one — here we use Instacart)
data("instacart")

# Randomly sample 3000 rows to make plots faster
instacart_small <- instacart |> sample_n(3000)

Add dashboard sections

Column 1: Bar Plot

### Orders by Department

instacart_small |> 
  count(department) |> 
  plot_ly(
    x = ~department,
    y = ~n,
    type = "bar",
    color = ~department
  ) |> 
  layout(
    title = "Number of Orders by Department",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Count of Orders")
  )
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

Column 2: Box Plot

### Add-to-Cart Order Distribution by Department
instacart_small |> 
  plot_ly(
    x = ~department,
    y = ~add_to_cart_order,
    type = "box",
    color = ~department
  ) |> 
  layout(
    title = "Add-to-Cart Order Distribution",
    xaxis = list(title = "Department"),
    yaxis = list(title = "Add-to-Cart Order")
  )
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

Column 3: Scatter Plot

### Aisle vs Add-to-Cart Order

instacart_small |> 
  plot_ly(
    x = ~aisle_id,
    y = ~add_to_cart_order,
    type = "scatter",
    mode = "markers",
    color = ~department
  ) |> 
  layout(
    title = "Aisle vs Add-to-Cart Order",
    xaxis = list(title = "Aisle ID"),
    yaxis = list(title = "Add-to-Cart Order")
  )
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(max(N, 3L), "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors